<template>

    <el-upload
            class="upload-demo"
            drag
            accept="image/jpeg,image/jog,image/png"
            action="http://localhost:8085/books/covers"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :before-remove="beforeRemove"
            :on-success="handleSuccess"
            multiple
            :limit="1"
            :on-exceed="handleExceed"
            :file-list="fileList">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
    </el-upload>

</template>

<script>
    export default {
        name: 'ImgUpload',
        data() {
            return {
                fileList: [],
                url: ''
            }
        },
        methods: {
            handleRemove(file, fileList) {
                console.log(file, fileList)
            },
            handlePreview(file) {
                console.log(file)
                // 此处的 file 是整个文件
                // console.log(file.response)
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
            },
            beforeRemove(file, fileList) {
                console.log(fileList)
                return this.$confirm(`确定移除 ${file.name}？`)
            },
            handleSuccess(response) {
                this.url = response
                // console.log(this.url)
                this.$emit('onUpload')
                this.$message.warning('上传成功')
            },
            clear() {
                this.$refs.upload.clearFiles()
            }
        }
    }
</script>

<style scoped>
    .img-upload {
        /*height: 200px;*/
    }
</style>
